package com.haochang.chunkvod.controller.fragment.hostess;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

/**
 * 咨客--预收/代付--预收fragment
 *     @author Yangjie
 *     className Fragment_add_reserve_room
 *     created at  2016/11/1  17:54
 */
public class AdvancesReceivedFragment extends Fragment implements KeyBoardDialog.KeyBoardOnSureListener
{
	private Context mContext;
	private View view;// infalte的布局
	private LinearLayout myContainer;// 新建容器
	////////////////////////////////
	private BaseTextView tv_cash, tv_membership_card, tv_swiping_card;//弹出数字键盘
	private BaseTextView clickedView;//记录点击弹出数字键盘的view
	private BaseEditText et_content;
	private RadioGroup rg_fee_type;//单选按钮控制器

	private HostessData hostessData;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mContext = getActivity();
		hostessData = new HostessData((BaseActivity) mContext);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		if (null == view)
		{
			myContainer = new LinearLayout(mContext);
			view = inflater.inflate(R.layout.fragment_advanced_received, container, false);
			view.setMinimumHeight(((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight());
			view.setMinimumWidth(((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth());
			myContainer.addView(view);
		} else
		{
			myContainer.removeAllViews();
			myContainer = new LinearLayout(getActivity());
			myContainer.addView(view);
		}
		initView();
		initData();
		return myContainer;
	}

	private void initView()
	{
		rg_fee_type = (RadioGroup) view.findViewById(R.id.rg_fee_type);

		et_content = (BaseEditText) view.findViewById(R.id.et_content);

		tv_cash = (BaseTextView) view.findViewById(R.id.tv_cash);
		tv_membership_card = (BaseTextView) view.findViewById(R.id.tv_membership_card);
		tv_swiping_card = (BaseTextView) view.findViewById(R.id.tv_swiping_card);
	}

	private void initData()
	{
		rg_fee_type.setOnCheckedChangeListener(onCheckedChangeListener);

		tv_cash.setOnClickListener(clickShowDigitalKeyBoard);
		tv_membership_card.setOnClickListener(clickShowDigitalKeyBoard);
		tv_swiping_card.setOnClickListener(clickShowDigitalKeyBoard);
	}

	public void requestAddAdvancesReceived()
	{
		if (isLegal())
			hostessData.requestAddAdvancesReceived(et_content.getText().toString(), rg_fee_type.getCheckedRadioButtonId() == R.id.rb_fee_of_layout ? "0" : "1",
					tv_cash.getText().toString(), tv_membership_card.getText().toString(), tv_swiping_card.getText().toString(),
					new HostessData.ResultResponse()
					{
						@Override
						public void onRequestSuccess()
						{
							ToastUtils.showText(mContext, "添加预收成功");
						}

						@Override
						public void onFail(int errorNo, String errorMsg)
						{
							ToastUtils.showText(mContext, TextUtils.isEmpty(errorMsg) ? "添加预收失败" : errorMsg);
						}
					});
	}

	/**
	 * @return 请求是否合法
	 */
	private boolean isLegal()
	{
		if (TextUtils.isEmpty(tv_cash.getText().toString()))
		{
			ToastUtils.showText(mContext, "请输入现金金额!");
			return false;
		}
		if (TextUtils.isEmpty(tv_membership_card.getText().toString()))
		{
			ToastUtils.showText(mContext, "请输入会员卡金额!");
			return false;
		}
		if (TextUtils.isEmpty(tv_swiping_card.getText().toString()))
		{
			ToastUtils.showText(mContext, "请输入刷卡金额!");
			return false;
		}
		return true;
	}

	OnBaseClickListener clickShowDigitalKeyBoard = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			clickedView = (BaseTextView) v;
			KeyBoardDialog keyBoardDialog = new KeyBoardDialog(mContext);
			keyBoardDialog.setKeyBoardOnSureListener(AdvancesReceivedFragment.this);
			keyBoardDialog.show();
		}
	};

	RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener()
	{
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId)
		{

		}
	};

	@Override
	public void setValue(String value)
	{
		DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance();
		decimalFormat.applyPattern("0.00");
		try
		{
			clickedView.setText(decimalFormat.format(Double.parseDouble(value)));
		} catch (Exception e)
		{
			ToastUtils.showText(mContext, getResources().getString(R.string.error_max_number));
		}
	}
}
